package com.guli.gulimall.guliessearch.controller;
import com.guli.common.utils.R;
import com.guli.gulimall.guliessearch.constant.CodeEnume;
import com.guli.gulimall.guliessearch.service.ProductSavaService;
import com.guli.to.es.SkuEsModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("search")
@Api("ES搜索引擎相关")
@Slf4j
public class ElasticSave {

    @Autowired
    RestHighLevelClient restHighLevelClient;
    @Autowired
    ProductSavaService productSavaService;
    @ApiOperation("保存数据")
    @PostMapping("/sava")
    public R elasticSava(@RequestBody  List<SkuEsModel> esModels) throws IOException {
        System.out.println(esModels);
        boolean Bfalse = false;
        try {

            Bfalse = productSavaService.productStatusUp(esModels);

        }catch (Exception e){

            return  R.error(CodeEnume.PRODUCT_UP_EXCEPTION.getCode(),CodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        if(Bfalse){
            log.error("商品未能成功保存，非系统原因");
            return  R.error(CodeEnume.PRODUCT_UP_EXCEPTION.getCode(),CodeEnume.PRODUCT_UP_EXCEPTION.getMsg());
        }
        return  R.ok(0,"success");
    }
}
